package com.eqdd.yiqidian.ui.mine.shenqing;

import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.bigkoo.pickerview.TimePickerView;
import com.eqdd.yiqidian.ApplayLizhiActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.choose.HangyeBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditHintBean;
import com.eqdd.yiqidian.bean.utils.MultiChooseBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity;
import com.eqdd.yiqidian.utils.MultiChooseTestUtil;
import com.eqdd.yiqidian.utils.SingleChooseUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.widget.ActionSheetDialog;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.model.Response;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import chihane.jdaddressselector.BottomDialog;

public class ApplyLizhiActivity extends SlimBaseMultiEditActivity {
    private ApplayLizhiActivityCustom dataBinding;
    private ActionSheetDialog chooseDialogLeixing;
    private ActionSheetDialog chooseDialogYuanyin;
    private TimePickerView pvDayTime;
    private List<HangyeBean> hangyeBeen;
    private BottomDialog lizhiyuanyinDialog;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_applay_lizhi);
        initTopTitleBar(View.VISIBLE, "离职申请");
        initTopRightText("提交", v -> {
            OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.APPALY_LIZHI)
                    .params("companyId", user.getCompanyId())
                    .params("postId", user.getPostId())
                    .params("departId", user.getDepartId())
                    .params("userGuid", user.getGuid())
                    .params("joinNumber", user.getJobNumber())
                    .params("joinTime", setParam(4))
                    .params("quitTime", datas.get(5).getContent().trim())
                    .params("quitType", datas.get(6).getContent().trim())
                    .params("quitReason", datas.get(7).getContent().trim())
                    .execute(new DialogCallBack<HttpResult>(this) {
                        @Override
                        public void onSuccess(Response<HttpResult> response) {
                            HttpResult httpResult = response.body();
                            ToastUtil.showShort(httpResult.getMsg());
                            if (httpResult.getStatus() == 200) {
                                isSave = false;
                                clearData();
                                setResult(Config.SUCCESS);
                                finish();
                            }
                        }
                    });
        });
    }


    @Override
    protected RecyclerView initRecyclerView() {
        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {
        switch (p) {
            case 5:
                if (pvDayTime == null) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(new Date());
                    calendar.add(Calendar.DAY_OF_YEAR, 15);
                    Calendar calendarEnd = Calendar.getInstance();
                    calendarEnd.set(2050, 11, 31);
                    pvDayTime = new TimePickerView.Builder(this, (date, v) -> {
                        SimpleDateFormat sf = null;
                        sf = new SimpleDateFormat("yyyy-MM-dd");
                        datas.get(p).setContent(sf.format(date));
                        slimAdapterEx.notifyItemChanged(p);
                    })
                            .setOutSideCancelable(true)
                            .setType(Config.YEAR_MONTH_DAY_TYPE)
                            .setDate(Calendar.getInstance())
                            .setRangDate(calendar, calendarEnd)
                            .build();
                }
                pvDayTime.show();
                break;
            case 6:
                if (chooseDialogLeixing == null) {
                    SingleChooseUtil.getChooseDialog(this, Config.LIZHILEIXING, (sheet) -> {
                        chooseDialogLeixing = sheet;
                        sheet.show();
                    }, (position, strings) -> {
                        datas.get(p).setContent(strings.get(position - 1));
                        slimAdapterEx.notifyItemChanged(p);
                    });
                } else {
                    chooseDialogLeixing.show();
                }
                break;
            case 7:
                lizhiyuanyinDialog.show();
                break;
        }
    }

    @Override
    protected void setEditBeans() {
        datas.add(new SlimEditHintBean("申请人", user.getUsername(), 0));
        datas.add(new SlimEditHintBean("所在部门", user.getDepartment(), 1));
        datas.add(new SlimEditHintBean("所在岗位", user.getPost(), 2));
        datas.add(new SlimEditHintBean("工号", user.getJobNumber(), 3));
        datas.add(new SlimEditHintBean("入职时间", AppConstent.user.getSignEntryTime(), 4));
        datas.add(new SlimEditEnterBean("预计离职时间", "", 5, true, "请选择"));
        datas.add(new SlimEditEnterBean("离职类型", "", 6, true, "请选择"));
        datas.add(new SlimEditEnterBean("离职原因", "", 7, true, "请选择"));
        OkGo.<List<HangyeBean>>post(HttpConfig.BASE_URL + HttpConfig.AREA_OPTIONS)
                .cacheKey(HttpConfig.BASE_URL + HttpConfig.AREA_OPTIONS + Config.LIZHIYUANYIN)
                .cacheMode(CacheMode.IF_NONE_CACHE_REQUEST)
                .params("type", Config.LIZHIYUANYIN)
                .execute(new DialogCallBack<List<HangyeBean>>(this) {
                    @Override
                    public void onSuccess(Response<List<HangyeBean>> response) {
                        hangyeBeen = response.body();
                        initLizhiyuanyinPicker();
                    }

                    @Override
                    public void onCacheSuccess(Response<List<HangyeBean>> response) {
                        super.onCacheSuccess(response);
                        hangyeBeen = response.body();
                        initLizhiyuanyinPicker();

                    }
                });
    }

    private void initLizhiyuanyinPicker() {
        ArrayList<MultiChooseBean> multiChooseBeen = new ArrayList<>();
        multiChooseBeen.addAll(hangyeBeen);
        lizhiyuanyinDialog = new MultiChooseTestUtil().getAreaDialog(this, 2, multiChooseBeen, (selectIds, selectAbles) -> {
            lizhiyuanyinDialog.hide();
            datas.get(7).setContent(selectAbles.get(1).getName());
            slimAdapterEx.notifyItemChanged(7);
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }
}
